﻿package com.schimanek.navigation.tree 
{
	import de.polygonal.ds.TreeNode;
	
	/**
	 * ...
	 * @author Gregor Schimanek
	 */
	public class Tree
	{
		public var tree:TreeNode;
		private var xNodeName:String;
		private var xmlData:XML;
		
		public function Tree() 
		{
						
		}
		
		public function buildTree(xdt:XML, xNodeName:String):void {
			tree = new TreeNode();
			xmlData = xdt;
			this.xNodeName = xNodeName;
			buildTreeNode(xdt, tree)
		}
		
		
		private function buildTreeNode(nodeXML:XML,parent:TreeNode):void
		{			
			var xList:XMLList = nodeXML[xNodeName];
			
			var lastNode:TreeNode;
			var currentNode:TreeNode;
			
			for (var i:uint = 0; i < xList.length(); i++) {
				//MAKE A NEW NODE			
				currentNode = new TreeNode(null, parent);				
				
				//RECURSE IF NODE HAS CHILDREN:
				var xChild:XML = xList[i];
				if(xChild!=""){					
					buildTreeNode(xChild, currentNode);
				}				
			}		
		}
		
	}
	
}